package cn.xk.basic.controller;


import cn.xk.basic.exception.BusinessException;
import cn.xk.basic.service.IVerifyCodeService;
import cn.xk.basic.util.AjaxResult;
import cn.xk.dto.PhoneCodeDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {

    @Autowired
    private IVerifyCodeService verifyCodeService;

    @GetMapping("/image/{key}")
    public AjaxResult getImageCode(@PathVariable("key") String key) {
        try {
            String base64Str = verifyCodeService.getImageCode(key);
            return AjaxResult.me().setObj(base64Str);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "网络繁忙，请稍后重试");
        }
    }

    /**
     * 发送手机短信验证码
     *
     * @param phoneCodeDto
     * @return
     */
    @PostMapping("/phone")
    public AjaxResult getPhoneCode(@RequestBody PhoneCodeDto phoneCodeDto) {
        try {
            verifyCodeService.getPhoneCode(phoneCodeDto);
            return AjaxResult.me();
        } catch (BusinessException e) {
            return new AjaxResult(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "网络繁忙，请稍后重试");
        }
    }
    @PostMapping("/binderSmsCode")
    public AjaxResult BinderSmsCode(@RequestBody PhoneCodeDto phoneCodeDto) {
        try {
            verifyCodeService.getBinderSmsCode(phoneCodeDto);
            return AjaxResult.me();
        } catch (BusinessException e) {
            return new AjaxResult(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "网络繁忙，请稍后重试");
        }
    }
}
